/*
* $Id: AddInspectView.cpp 68 2010-03-18 19:49:39Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "AddInspectView.h"
#include "ViewFieldValuesHelper.h"
#include "Visual/Drawers/EditorDrawer/HeaderItem.h"
#include "Visual/Drawers/EditorDrawer/EditItem.h"
#include "Visual/Drawers/EditorDrawer/ComboBoxItem.h"
#include "Visual/Drawers/EditorDrawer/SectionItem.h"
#include "Database/Tables/JournalTable.h"
#include "Database/Tables/HousingsTable.h"
#include "Database/Operation/AddInspectOperation.h"
#include "Settings/Journal/RecordViewSettings.h"
#include "Settings/SettingControlIds.h"
#include "Settings/DinIntDefined.h"

#define TITLE_ID				1
#define DATE_INSPECT_ID			2
#define FRAME_INSPECT_ID		3
#define STRENGTH_INSPECT_ID		4
#define EBQ_INSPECT_ID			5
#define WAX_INSPECT_ID			6
#define AMBROSIA_INSPECT_ID		7
#define PUMP_INSPECT_ID			8
#define FEEDING_INSPECT_ID		9
#define DISEASES_INSPECT_ID		10
#define NOTE_INSPECT_ID			11
#define USER_FIELD_ID			12
#define HOUSING_FIELD_ID(i, j)	(i<<8)|j
#define HONEY_FIELD_ID			1
#define BROOD_FIELD_ID			2
#define OPEN_BROOD_FIELD_ID		3
#define CLOSE_BROOD_FIELD_ID	4
#define GOOGS_HONEY_FIELD_ID	5

CAddInspectView::CAddInspectView(CState* pState)
: CAddChangeView(pState), m_nUserFields(0){}
CAddInspectView::~CAddInspectView(){}

void CAddInspectView::Init(){
	m_editorDrawer.Clear();
	m_aHousings.clear();
	m_editorDrawer.AddItem(new CHeaderItem(IApplication::GetApp()->GetLanguageManager()->GetString(ADD_INSPECT_ID), TITLE_ID));
	m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_DATE_ID), DATE_INSPECT_ID, editItem::DATE));
	m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_FRAME_ID), FRAME_INSPECT_ID, editItem::INTEGER));
	m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_BEES_ID), STRENGTH_INSPECT_ID, editItem::INTEGER));
	CComboBoxItem* pItem = new CComboBoxItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_EBQ_ID), EBQ_INSPECT_ID);
	pItem->AddValueItem(CJournalTable::content::DEFAULT, IApplication::GetApp()->GetLanguageManager()->GetString(QUEEN_BEE_STATUS_DEFAULT_ID));
	pItem->AddValueItem(CJournalTable::content::LOOK, IApplication::GetApp()->GetLanguageManager()->GetString(QUEEN_BEE_STATUS_LOOK_ID));
	pItem->AddValueItem(CJournalTable::content::ABSENCE, IApplication::GetApp()->GetLanguageManager()->GetString(QUEEN_BEE_STATUS_ABSENCE_ID));
	pItem->AddValueItem(CJournalTable::content::FETAL, IApplication::GetApp()->GetLanguageManager()->GetString(QUEEN_BEE_STATUS_FETAL_ID));
	m_editorDrawer.AddItem(pItem);
	CRecordViewSettings jSettings;
	if(jSettings.GetCurrentValue(CHECK_GIVE_WOX_ID) == CHECK_BOX_STATE_1)
		m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_WAX_ID), WAX_INSPECT_ID, editItem::INTEGER));
	if(jSettings.GetCurrentValue(CHECK_AMBROSIA_ID) == CHECK_BOX_STATE_1)
		m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_AMBROSIA_ID), AMBROSIA_INSPECT_ID, editItem::INTEGER));
	if(jSettings.GetCurrentValue(CHECK_PUMP_HONEY_ID) == CHECK_BOX_STATE_1)
		m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_PUMP_HONEY_ID), PUMP_INSPECT_ID, editItem::INTEGER));
	if(jSettings.GetCurrentValue(CHECK_FEEDING_ID) == CHECK_BOX_STATE_1)
		m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_FEEDING_ID), FEEDING_INSPECT_ID, editItem::INTEGER));
	if(jSettings.GetCurrentValue(CHECK_DISEASES_ID) == CHECK_BOX_STATE_1){
		CComboBoxItem* pItem = new CComboBoxItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_DISEASES_ID), DISEASES_INSPECT_ID);
		for(int i = 0, id = DISEASE_HEALTHY_ID; i < 33; i++, id++)
			pItem->AddValueItem(i, IApplication::GetApp()->GetLanguageManager()->GetString(id));
		m_editorDrawer.AddItem(pItem);
	}
	if(jSettings.GetCurrentValue(CHECK_NOTE_ID) == CHECK_BOX_STATE_1)
		m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_NOTE_ID), NOTE_INSPECT_ID, editItem::TEXT));
	CViewFieldValuesHelper helper(&m_editorDrawer);
	m_nUserFields = helper.GenerateUserEditItems(CViewFieldValuesHelper::INSPECT, USER_FIELD_ID, 0);

	IDatabase * pDb = IApplication::GetApp()->GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	CHousingsTable housing;
	housing.m_content.s_nIdFamily = m_nIdParameter;
	IResult* pResult = housing.GetValue(pDb);
	for(int i = 0, j = 1; i < (int)pResult->GetSizeResult(); i++, j++){
		CHousingsTable::content* pContent = (CHousingsTable::content*)(*pResult)[i];
		m_aHousings.insert(std::make_pair(j, pContent->s_nId));
		m_editorDrawer.AddItem(new CSectionItem(IApplication::GetApp()->GetLanguageManager()->GetString(HOUSING_ID) + _T(" ") + pContent->s_strName, HOUSING_FIELD_ID(j, 0)));
		m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_BROOD_ID), HOUSING_FIELD_ID(j, BROOD_FIELD_ID), editItem::INTEGER));
		m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_HONEY_ID), HOUSING_FIELD_ID(j, HONEY_FIELD_ID), editItem::INTEGER));
		if(jSettings.GetCurrentValue(CHECK_OPEN_BROOD_ID) == CHECK_BOX_STATE_1)
			m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_OPEN_BROOD_ID), HOUSING_FIELD_ID(j, OPEN_BROOD_FIELD_ID), editItem::INTEGER));
		if(jSettings.GetCurrentValue(CHECK_SEAL_BROOD_ID) == CHECK_BOX_STATE_1)
			m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_CLOSE_BROOD_ID), HOUSING_FIELD_ID(j, CLOSE_BROOD_FIELD_ID), editItem::INTEGER));
		if(jSettings.GetCurrentValue(CHECK_GOODS_HONEY_ID) == CHECK_BOX_STATE_1)
			m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_GOODS_HONEY_ID), HOUSING_FIELD_ID(j, GOOGS_HONEY_FIELD_ID), editItem::INTEGER));
	}
	pResult->Free();
	IApplication::GetApp()->GetDatabaseManager()->CloseDatabase(pDb);
}

void CAddInspectView::SetState(){
	m_pState->m_nState = JournalEnum::VIEW_ADD_INSPECT;
}

bool CAddInspectView::IsViewState(State state){
	return state.m_type == JournalEnum::ADD_INSPECT;
}

BEGIN_MESSAGE_MAP(CAddInspectView, CAddChangeView)
	ON_COMMAND(BUTTON_SAVE_ID, OnSaveInspect)
END_MESSAGE_MAP()

void CAddInspectView::OnSaveInspect(){
	IDatabase* pDb = IApplication::GetApp()->GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	CAddInspectOperation operation;
	operation.m_content.s_nIdFamily = m_nIdParameter;
	operation.m_content.s_date = CConvertFunction::ConvertFromDate(m_editorDrawer.GetValueItem(DATE_INSPECT_ID));
	operation.m_content.s_nFrame = _ttoi(m_editorDrawer.GetValueItem(FRAME_INSPECT_ID).c_str());
	operation.m_content.s_nStrength = _ttoi(m_editorDrawer.GetValueItem(STRENGTH_INSPECT_ID).c_str());
	operation.m_content.s_nWax = _ttoi(m_editorDrawer.GetValueItem(WAX_INSPECT_ID).c_str());
	operation.m_content.s_beeQueenStatus = _ttoi(m_editorDrawer.GetValueItem(EBQ_INSPECT_ID).c_str());
	operation.m_content.s_nAmbrosia = _ttoi(m_editorDrawer.GetValueItem(AMBROSIA_INSPECT_ID).c_str());
	operation.m_content.s_nPumpHoney = _ttoi(m_editorDrawer.GetValueItem(PUMP_INSPECT_ID).c_str());
	operation.m_content.s_nFeeding = _ttoi(m_editorDrawer.GetValueItem(FEEDING_INSPECT_ID).c_str());
	operation.m_content.s_disease = _ttoi(m_editorDrawer.GetValueItem(DISEASES_INSPECT_ID).c_str());
	operation.m_content.s_strNote = m_editorDrawer.GetValueItem(NOTE_INSPECT_ID);
	for(int i = 1; i <= m_nUserFields; i++)
		operation.m_content.s_aUserFields.insert(std::make_pair(m_editorDrawer.GetNameItem(USER_FIELD_ID + i), m_editorDrawer.GetValueItem(USER_FIELD_ID + i)));
	for(std::map<int, int>::iterator it = m_aHousings.begin(); it != m_aHousings.end(); it++){
		int nBrood = _ttoi(m_editorDrawer.GetValueItem(HOUSING_FIELD_ID(it->first, BROOD_FIELD_ID)).c_str());
		int nHoney = _ttoi(m_editorDrawer.GetValueItem(HOUSING_FIELD_ID(it->first, HONEY_FIELD_ID)).c_str());
		int nOpenBrood = _ttoi(m_editorDrawer.GetValueItem(HOUSING_FIELD_ID(it->first, OPEN_BROOD_FIELD_ID)).c_str());
		int nCloseBrood = _ttoi(m_editorDrawer.GetValueItem(HOUSING_FIELD_ID(it->first, CLOSE_BROOD_FIELD_ID)).c_str());
		int nGoodsHoney = _ttoi(m_editorDrawer.GetValueItem(HOUSING_FIELD_ID(it->first, GOOGS_HONEY_FIELD_ID)).c_str());
		operation.m_content.s_aInspects.push_back(CAddInspectOperation::inspect(it->second, nBrood, nHoney, nOpenBrood, nCloseBrood, nGoodsHoney));
	}
	operation.AddValue(pDb);
	IApplication::GetApp()->GetDatabaseManager()->CloseDatabase(pDb);
	__super::OnCancel();
}